package com.cdwh.joylife.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.cdwh.joylife.App;
import com.cdwh.joylife.R;
import com.cdwh.joylife.ui.base.BaseActivity;
import com.cdwh.joylife.ui.fragment.HomeFragment;
import com.cdwh.joylife.ui.fragment.MeFragment;
import com.cdwh.joylife.ui.fragment.ShopFragment;
import com.cdwh.joylife.ui.fragment.StoreFragment;
import com.cdwh.joylife.utils.AppToast;
import com.cdwh.joylife.utils.BottomNavigationViewHelper;
import com.cdwh.joylife.utils.SPUtils;
import com.cdwh.joylife.utils.UiUtils;
import com.socks.library.KLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import qiu.niorgai.StatusBarCompat;

public class HomeActivity extends BaseActivity {

//    @BindView(R.id.titleBar)
//    TitleBar titleBar;
    @BindView(R.id.container)
    FrameLayout container;
    @BindView(R.id.navigation)
    BottomNavigationView navigation;


    private List<Fragment> fragments;
    private int lastShowFragment = 0;
    private HomeFragment homeFragment;
    private ShopFragment shopFragment;
    private StoreFragment storeFragment;
    private MeFragment meFragment;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    if (lastShowFragment != 0) {
                        switchFrament(lastShowFragment, 0);
                        lastShowFragment = 0;
                    }
                    SPUtils.put("lastShowFragment", lastShowFragment);
                    return true;
                case R.id.navigation_shop:
                    if (lastShowFragment != 1) {
                        switchFrament(lastShowFragment, 1);
                        lastShowFragment = 1;
                    }
                    SPUtils.put("lastShowFragment", lastShowFragment);
                    return true;
                case R.id.navigation_store:
                    if (lastShowFragment != 2) {
                        switchFrament(lastShowFragment, 2);
                        lastShowFragment = 2;
                    }
                    SPUtils.put("lastShowFragment", lastShowFragment);
                    return true;
                case R.id.navigation_me:
                    if (lastShowFragment != 3) {
                        switchFrament(lastShowFragment, 3);
                        lastShowFragment = 3;
                    }
                    SPUtils.put("lastShowFragment", lastShowFragment);
                    return true;
            }
            return false;
        }
    };


    @Override
    protected void setContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
//        titleBar.setPageBackIconGone(true);
        initFragments(savedInstanceState);
    }

    private void initFragments(Bundle savedInstanceState) {

        fragments = new ArrayList<>();
        if (savedInstanceState == null) {
            // Instantiate fragment
            container.removeAllViews();
            homeFragment = new HomeFragment();
            shopFragment = new ShopFragment();
            storeFragment = new StoreFragment();
            meFragment = new MeFragment();
            lastShowFragment = 0;
            SPUtils.put("lastShowFragment", lastShowFragment);
            switchFrament(0, 0);

        } else {
            // Restore the fragment's instance
            homeFragment = (HomeFragment) getSupportFragmentManager().getFragment(
                    savedInstanceState, "homeFragment");
            shopFragment = (ShopFragment) getSupportFragmentManager().getFragment(
                    savedInstanceState, "shopFragment");
            storeFragment = (StoreFragment) getSupportFragmentManager().getFragment(
                    savedInstanceState, "storeFragment");
            meFragment = (MeFragment) getSupportFragmentManager().getFragment(
                    savedInstanceState, "meFragment");
            lastShowFragment = savedInstanceState.getInt("lastShowFragment");
            SPUtils.put("lastShowFragment", lastShowFragment);
            switchFrament(lastShowFragment, lastShowFragment);
            KLog.e("页面重绘，lastShowFragment" + lastShowFragment);
        }
    }

    /**
     * 切换Fragment
     *
     * @param lastIndex 上个显示Fragment的索引
     * @param index     需要显示的Fragment的索引
     */
    public void switchFrament(int lastIndex, int index) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        if (fragments.isEmpty()) {
            container.removeAllViews();
            homeFragment = new HomeFragment();
            shopFragment = new ShopFragment();
            storeFragment = new StoreFragment();
            meFragment = new MeFragment();
            fragments.add(homeFragment);
            fragments.add(shopFragment);
            fragments.add(storeFragment);
            fragments.add(meFragment);
        }

        transaction.hide(fragments.get(lastIndex));
        if (!fragments.get(index).isAdded()) {
            if (index == 0 && manager.findFragmentByTag("homeFragment") != null) {
                transaction.add(R.id.container, fragments.get(index));
            } else {
                transaction.add(R.id.container, fragments.get(index));
            }

        }

//
//        switch (index) {
//            case 0:
//                StatusBarCompat.translucentStatusBar(this);
//                break;
//            case 1:
////                titleBar.setPageTitleTextColor(UiUtils.getColor(R.color.color_fff));
////                titleBar.setPageTitleText("消息");
//                StatusBarCompat.setStatusBarColor(this, UiUtils.getColor(R.color.colorPrimary));
//                break;
//            case 2:
////                titleBar.setTitleColor(UiUtils.getColor(R.color.colorPrimary));
////                titleBar.setPageTitleTextColor(UiUtils.getColor(R.color.color_fff));
////                titleBar.setPageTitleText("共享运维装备");
//                StatusBarCompat.setStatusBarColor(this, UiUtils.getColor(R.color.colorPrimary));
//                break;
//            case 3:
//                StatusBarCompat.setStatusBarColor(this, UiUtils.getColor(R.color.colorPrimary));
//                break;
//        }

        transaction.show(fragments.get(index)).commitAllowingStateLoss();
    }

    @Override
    protected void initData() {
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        BottomNavigationViewHelper.disableShiftMode(navigation);
    }

    @Override
    public void onReceive(Object data) {

    }

    @Override
    public void onReceive(Object data, int type) {

    }

    //退出时的时间
    private long mExitTime;

    //对返回键进行监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            AppToast.makeToast("再按一次退出" + UiUtils.getString(R.string.app_name));
            mExitTime = System.currentTimeMillis();
        } else {
            App.getActivityManager().finishAll();
        }
    }


//    @OnClick({R.id.btnCity, R.id.btnSearch})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.btnCity:
//                break;
//            case R.id.btnSearch:
//                break;
//        }
//    }
}
